48
Initialisation of Point class
Example
class Point {
int _x, _y;
public:
Point() {
_x = _y = 0;
}
void setX(const int val);
void setY(const int val);
int getX() { return _x; }
int getY() { return _y; }
};
Class Point initializes a point to coordinates (0, 0):
class Point {
int _x, _y;
public:
Point() {
_x = _y = 0;
}
Point(const int x, const int y) {
_x = x;
_y = y;
}
void setX(const int val);
void setY(const int val);
int getX() { return _x; }
int getY() { return _y; }
};

| Constructors have the same name of the class (thus they are identified to be constructors). They have no return value. As other methods, they can take arguments.
| Constructors have the same name of the class (thus they are identified to be constructors). They have no return value. As other methods, they can take arguments.
| For example, we may want to initialize a point to other coordinates than (0, 0). We therefore define a second constructor taking two integer arguments within the class.